function $$(id){
	return document.getElementById(id);
}
var lists;
var len;

window.onload = function(){
	var objtxt = $$("email");
	var objlist = $$("maillist");
	
	lists = objlist.getElementsByTagName("li");
	//移除第一个li
	len = lists.length;
	
	objlist.style.top = (objtxt.offsetTop + objtxt.offsetHeight) + "px";
	objlist.style.left = objtxt.offsetLeft + "px";
	
	objtxt.onfocus = function(){
		var txt = this.value;
		if(txt.indexOf("@") > 0){ //如果用户输入了@，则只取前面的数据
			txt = txt.substring(0, txt.indexOf("@"));
		}
		addtxt(txt);
		objlist.style.display = "block";
	}
	
	objtxt.onblur = function(){
		objlist.style.display = "none";
		showAll();
	}
	
	objtxt.onkeyup = function(){
		var txt = this.value;
		var flag = txt;
		if(txt.indexOf("@") >= 0){ //如果用户输入了@，则只取前面的数据
			txt = txt.substring(0, txt.indexOf("@"));
            searchs(flag);
		}
		addtxt(txt);
	}
	
	for(var i = 1; i< len; i ++){
		lists[i].onmousedown = function(){
			$$("email").value = this.innerHTML;
			$$("maillist").style.display = "none";
		}
	}
}

function showAll(txt){
	for(var i = 1; i < len; i ++){
		lists[i].style.display = "block";
	}
}

function searchs(txt){
	var reg = new RegExp(txt, "i");
	for(var i = 1; i < len; i++){
		if(reg.test(lists[i].textContent)){
			lists[i].style.display = "block";
		}else{
			lists[i].style.display = "none";
		}
	}
}

function addtxt(txt){
	var val;
	for(var i = 1; i < len; i ++){
		val = lists[i].innerHTML;
		val = val.substring(val.indexOf("@"));
		lists[i].innerHTML = txt+val;
	}
}